package com.zhangshanfeng.test;

import java.util.Objects;

public class Student2 implements Comparable<Student2> {
    private String name;
    private int age;

    public Student2() {
    }

    public Student2(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student2{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student2 student2 = (Student2) o;
        return age == student2.age &&
                Objects.equals(name, student2.name);
    }

    @Override
    public int hashCode() {
        //如果姓名不为空，返回name的hashCodo值
        int result = name != null ? name.hashCode() : 0;
        //重新给resule赋值，算是根据类的属性得到hashcode值
        result = 31 * result + age;

        return result;
    }

    @Override
    public int compareTo(Student2 o) {
        //要求按照学生的年龄进行排序,如果年龄相同则按照姓名进行排序
        int result = this.age - o.age;
        result = result == 0 ? this.name.compareTo(o.name) : result;
        return result;



    }
}


